﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:PerformancePrj
    /// </summary>
    public partial class PerformancePrj
    {
        public PerformancePrj()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FPerformancePrjID, string FInsEmpNo, string FEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_PerformancePrj");
            strSql.Append(" where FPerformancePrjID=@FPerformancePrjID and FInsEmpNo=@FInsEmpNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPerformancePrjID", SqlDbType.Decimal,13),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = FPerformancePrjID;
            parameters[1].Value = FInsEmpNo;
            parameters[2].Value = FEntityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ElingOA6_Admin_DSJCJ.Model.PerformancePrj model, List<Model.PerformancePrjEntry> PerformancePrjEntryList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_PerformancePrj(");
            strSql.Append("FYear,FSeq,FPrjName,FRelevantDept,FExecDept,FStatus,FProcess,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP)");
            strSql.Append(" values (");
            strSql.Append("@FYear,@FSeq,@FPrjName,@FRelevantDept,@FExecDept,@FStatus,@FProcess,@FEntityNo,@FInsEmpNo,@FInsDate,@FInsIP,@FUpdEmpNo,@FUpdDate,@FUpdIP)");
            strSql.Append(";select @returnBId=IDENT_CURRENT('dbo.t_PerformancePrj')");
            SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FSeq", SqlDbType.Int,4),
					new SqlParameter("@FPrjName", SqlDbType.NVarChar,200),
					new SqlParameter("@FRelevantDept", SqlDbType.NVarChar,200),
					new SqlParameter("@FExecDept", SqlDbType.NVarChar,200),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,200),
					new SqlParameter("@FProcess", SqlDbType.NVarChar,200),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15),
					new SqlParameter("@returnBId", SqlDbType.Decimal)};
            parameters[0].Value = model.FYear;
            parameters[1].Value = model.FSeq;
            parameters[2].Value = model.FPrjName;
            parameters[3].Value = model.FRelevantDept;
            parameters[4].Value = model.FExecDept;
            parameters[5].Value = model.FStatus;
            parameters[6].Value = model.FProcess;
            parameters[7].Value = model.FEntityNo;
            parameters[8].Value = model.FInsEmpNo;
            parameters[9].Value = model.FInsDate;
            parameters[10].Value = model.FInsIP;
            parameters[11].Value = model.FUpdEmpNo;
            parameters[12].Value = model.FUpdDate;
            parameters[13].Value = model.FUpdIP;
            parameters[14].Direction = ParameterDirection.Output;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            foreach (Model.PerformancePrjEntry performancePrjEntryModel in PerformancePrjEntryList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_PerformancePrjEntry(");
                strSql.Append("FPerformancePrjID,FAttachSvrFileName,FAttachOldFileName,FAttachDesc,FAttachFileExt,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FPerformanceFileClsCd,FIsPublic)");
                strSql.Append(" values (");
                strSql.Append("@FPerformancePrjID,@FAttachSvrFileName,@FAttachOldFileName,@FAttachDesc,@FAttachFileExt,@FEntityNo,@FInsEmpNo,@FInsDate,@FInsIP,@FPerformanceFileClsCd,@FIsPublic)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FPerformancePrjID", SqlDbType.Decimal,13),
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachDesc", SqlDbType.NVarChar,200),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FPerformanceFileClsCd", SqlDbType.NVarChar,3),
					new SqlParameter("@FIsPublic", SqlDbType.NVarChar,1)};
                parameters2[0].Direction = ParameterDirection.InputOutput;
                parameters2[1].Value = performancePrjEntryModel.FAttachSvrFileName;
                parameters2[2].Value = performancePrjEntryModel.FAttachOldFileName;
                parameters2[3].Value = performancePrjEntryModel.FAttachDesc;
                parameters2[4].Value = performancePrjEntryModel.FAttachFileExt;
                parameters2[5].Value = performancePrjEntryModel.FEntityNo;
                parameters2[6].Value = performancePrjEntryModel.FInsEmpNo;
                parameters2[7].Value = performancePrjEntryModel.FInsDate;
                parameters2[8].Value = performancePrjEntryModel.FInsIP;
                parameters2[9].Value = performancePrjEntryModel.FPerformanceFileClsCd;
                parameters2[10].Value = performancePrjEntryModel.FIsPublic;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);

            }


            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }

        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.PerformancePrj model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_PerformancePrj set ");
            strSql.Append("FYear=@FYear,");
            strSql.Append("FSeq=@FSeq,");
            strSql.Append("FPrjName=@FPrjName,");
            strSql.Append("FRelevantDept=@FRelevantDept,");
            strSql.Append("FExecDept=@FExecDept,");
            strSql.Append("FStatus=@FStatus,");
            strSql.Append("FProcess=@FProcess,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FUpdEmpNo=@FUpdEmpNo");
            strSql.Append(" where FPerformancePrjID=@FPerformancePrjID");
            SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FSeq", SqlDbType.Int,4),
					new SqlParameter("@FPrjName", SqlDbType.NVarChar,200),
					new SqlParameter("@FRelevantDept", SqlDbType.NVarChar,200),
					new SqlParameter("@FExecDept", SqlDbType.NVarChar,200),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,200),
					new SqlParameter("@FProcess", SqlDbType.NVarChar,200),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15),
					new SqlParameter("@FPerformancePrjID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FYear;
            parameters[1].Value = model.FSeq;
            parameters[2].Value = model.FPrjName;
            parameters[3].Value = model.FRelevantDept;
            parameters[4].Value = model.FExecDept;
            parameters[5].Value = model.FStatus;
            parameters[6].Value = model.FProcess;
            parameters[7].Value = model.FUpdDate;
            parameters[8].Value = model.FUpdIP;
            parameters[9].Value = model.FPerformancePrjID;
            parameters[10].Value = model.FEntityNo;
            parameters[11].Value = model.FUpdEmpNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
 
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(decimal FPerformancePrjID)
        {

            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();

            strSql.Append("delete from dbo.t_PerformancePrjEntry ");
            strSql.Append(" where FPerformancePrjID=@FPerformancePrjID");
            SqlParameter[] parameters2 = {
					new SqlParameter("@FPerformancePrjID", SqlDbType.Decimal)
			};
            parameters2[0].Value = FPerformancePrjID;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);


            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_PerformancePrj ");
            strSql.Append(" where FPerformancePrjID=@FPerformancePrjID");
            SqlParameter[] parameters = {
					new SqlParameter("@FPerformancePrjID", SqlDbType.Decimal)
			};
            parameters[0].Value = FPerformancePrjID;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);
            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FPerformancePrjID, string FInsEmpNo, string FEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_PerformancePrj ");
            strSql.Append(" where FPerformancePrjID=@FPerformancePrjID and FInsEmpNo=@FInsEmpNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPerformancePrjID", SqlDbType.Decimal,13),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = FPerformancePrjID;
            parameters[1].Value = FInsEmpNo;
            parameters[2].Value = FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FPerformancePrjIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_PerformancePrj ");
            strSql.Append(" where FPerformancePrjID in (" + FPerformancePrjIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.PerformancePrj GetModel(decimal FPerformancePrjID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPerformancePrjID,FYear,FSeq,FPrjName,FRelevantDept,FExecDept,FStatus,FProcess,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP from dbo.t_PerformancePrj ");
            strSql.Append(" where FPerformancePrjID=@FPerformancePrjID");
            SqlParameter[] parameters = {
					new SqlParameter("@FPerformancePrjID", SqlDbType.Decimal)
			};
            parameters[0].Value = FPerformancePrjID;

            ElingOA6_Admin_DSJCJ.Model.PerformancePrj model = new ElingOA6_Admin_DSJCJ.Model.PerformancePrj();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.PerformancePrj DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.PerformancePrj model = new ElingOA6_Admin_DSJCJ.Model.PerformancePrj();
            if (row != null)
            {
                if (row["FPerformancePrjID"] != null && row["FPerformancePrjID"].ToString() != "")
                {
                    model.FPerformancePrjID = decimal.Parse(row["FPerformancePrjID"].ToString());
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FSeq"] != null && row["FSeq"].ToString() != "")
                {
                    model.FSeq = int.Parse(row["FSeq"].ToString());
                }
                if (row["FPrjName"] != null)
                {
                    model.FPrjName = row["FPrjName"].ToString();
                }
                if (row["FRelevantDept"] != null)
                {
                    model.FRelevantDept = row["FRelevantDept"].ToString();
                }
                if (row["FExecDept"] != null)
                {
                    model.FExecDept = row["FExecDept"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FProcess"] != null)
                {
                    model.FProcess = row["FProcess"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FInsEmpNo"] != null)
                {
                    model.FInsEmpNo = row["FInsEmpNo"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
                if (row["FUpdEmpNo"] != null)
                {
                    model.FUpdEmpNo = row["FUpdEmpNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row.Table.Columns.Contains("Counts"))
                {
                    if (row["Counts"] != null)
                    {
                        model.Counts = row["Counts"].ToString();
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetYear()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("  SELECT DISTINCT FYear FROM dbo.t_PerformancePrj ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FPerformancePrjID,FYear,FSeq,FPrjName,FRelevantDept,FExecDept,FStatus,FProcess,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP ");
            strSql.Append(" FROM dbo.t_PerformancePrj ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FPerformancePrjID,FYear,FSeq,FPrjName,FRelevantDept,FExecDept,FStatus,FProcess,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP ");
            strSql.Append(" FROM dbo.t_PerformancePrj ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dbo.t_PerformancePrj ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FPerformancePrjID desc");
            }
            strSql.Append(")AS Row, T.*  from dbo.t_PerformancePrj T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_PerformancePrj";
            parameters[1].Value = "FPerformancePrjID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

